package com.springboot.cloud.demos.producer.rest;

import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.web.redis.CacheDuration;
import com.springboot.cloud.common.web.redis.ExpireTimeConstant;
import com.springboot.cloud.demos.entity.form.AlbumForm;
import com.springboot.cloud.demos.entity.param.MomentQueryParam;
import com.springboot.cloud.demos.entity.param.AlbumQueryParam;
import com.springboot.cloud.demos.entity.po.Album;
import com.springboot.cloud.demos.entity.po.Moment;
import com.springboot.cloud.demos.producer.service.IUnitService;
import com.springboot.cloud.demos.producer.service.IMomentService;
import com.springboot.cloud.demos.producer.service.IAlbumService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/album")
@Api("album")
@CacheDuration(duration = ExpireTimeConstant.ONE_DAY)
@Slf4j
public class AlbumController {

    @Autowired
    private IAlbumService albumService;

    @ApiOperation(value = "新增产品", notes = "新增一个产品")
    @ApiImplicitParam(name = "albumForm", value = "新增产品form表单", required = true, dataType = "AlbumForm")
    @PostMapping
    public Result add(@Valid @RequestBody AlbumForm albumForm) {
        log.info("name:{}", albumForm);
        Album userAlbum = albumForm.toPo(Album.class);
        return Result.success(albumService.add(userAlbum));
    }

    @ApiOperation(value = "新增产品", notes = "新增多个产品")
    @ApiImplicitParam(name = "albums", value = "新增产品form表单", required = true, dataType = "Album")
    @PostMapping("/batchAdd")
    public Result batchAdd(@Valid @RequestBody List<Album> albums) {
        return Result.success(albumService.batchAdd(albums));
    }

    @ApiOperation(value = "删除产品", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "long")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(albumService.delete(id));
    }

    @ApiOperation(value = "删除产品", notes = "根据url的id来指定删除对象")
    @PostMapping(value = "/deleteByUrl")
    public Result deleteByUrl(@RequestBody AlbumForm albumForm) {
        albumService.deleteByUrl(albumForm.getUrl());
        return Result.success();
    }

    @ApiOperation(value = "修改产品", notes = "修改指定产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "产品ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "userAlbumForm", value = "产品实体", required = true, dataType = "AlbumForm")
    })
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody AlbumForm userAlbumForm) {
        Album userAlbum = userAlbumForm.toPo(Album.class);
        userAlbum.setId(id);
        return Result.success(albumService.update(userAlbum));
    }

    @ApiOperation(value = "获取产品", notes = "获取指定产品信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "产品ID", required = true, dataType = "string")
    @GetMapping(value = "/{id}")
    public Result get(@PathVariable String id) {
        log.info("get with id:{}", id);
        return Result.success(albumService.get(id));
    }

    @ApiOperation(value = "查询产品", notes = "根据条件查询产品信息，简单查询")
    @ApiImplicitParam(paramType = "query", name = "momentId", value = "单元Id", required = true, dataType = "string")
    @ApiResponses(
            @ApiResponse(code = 200, message = "处理成功", response = Result.class)
    )
    @GetMapping
    public Result<List<Album>> query(@RequestParam String momentId) {
        AlbumQueryParam param = new AlbumQueryParam();
        param.setMomentId(momentId);
        List<Album> list = albumService.query(param);
        return Result.success(list);

    }

}

